package LeetCode._01_树;

public class _106_从中序与后序遍历序列构造二叉树 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        return build(inorder,0, inorder.length-1,
                postorder,0,postorder.length-1);
    }

    private TreeNode build(int[] inorder,int inStart,int inEnd,
                           int[] postorder,int postStart,int postEnd){
        if(inStart>inEnd || postStart>postEnd) return null;

        int rootVal = postorder[postEnd];
        int index = -1;
        for(int i=inStart;i<=inEnd;++i){
            if(rootVal == inorder[i]){
                index = i;
                break;
            }
        }

        int leftSize = index - inStart;
        TreeNode root = new TreeNode(rootVal);
        root.left = build(inorder,inStart,index-1,
                          postorder,postStart,postStart+leftSize-1);
        root.right = build(inorder,index+1,inEnd,
                           postorder,postStart+leftSize,postEnd-1);

        return root;
    }

}
